<?php
namespace Baseapp\Backend\Controllers;

use Baseapp\Library\Auth,
    Phalcon\Mvc\View,
    \Baseapp\Models\Users;

class ProfileController extends ControllerBase
{
    public function loginAction()
    {
         if ($this->request->isPost() == TRUE && $this->request->hasPost('username') && $this->request->hasPost('password')) {
            $login = Auth::instance()->login($this->request->getPost('username'), $this->request->getPost('password'), $this->request->getPost('remember') ? TRUE : FALSE);
            if (!$login) {
                $errors = new \Phalcon\Validation\Message\Group();
                if ($login === NULL)
                    $errors->appendMessage(new \Phalcon\Validation\Message(__('Incorrect username'), 'username', 'NotFound'));
                else
                    $errors->appendMessage(new \Phalcon\Validation\Message(__('Incorrect password'), 'password', 'Identical'));

                $this->view->setVar('errors', $errors);
                $this->flashSession->warning(
                        $this->tag->linkTo(array('#', 'class' => 'close', 'title' => __("Close"), '×')) .
                        '<strong>' . __('Warning') . '!</strong> ' .
                        __("Please correct the errors."));
            } else {
                    $this->response->redirect('admin/master');
                    return;
                }
        }

        // This is an Ajax response so it doesn't generate any kind of view
        //$this->view->setRenderLevel(View::LEVEL_NO_RENDER);

        // Shows only the view related to the action
        $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
    }

    public function logoutAction()
    {
         $this->response->redirect('admin/profile/login');
        // This is an Ajax response so it doesn't generate any kind of view
        $this->view->setRenderLevel(View::LEVEL_NO_RENDER);
    }
}